package com.app.service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;

public abstract class BaseService extends AsyncTask<String, Integer, String> {
	public static String url_base = "http://www.lijiang.com";
	public HttpCallBack callback;
	
	public BaseService(HttpCallBack callBack)
	{
		this.callback = callBack;
	}
	protected String connectUrl(String url) {
		InputStream is;
		try 
		{
			HttpGet httpGet = new HttpGet(URI.create(url));
            HttpClient httpclient = new DefaultHttpClient();
             
            HttpResponse response = httpclient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            long length = entity.getContentLength();
            is = entity.getContent();          
            if(is!=null)
            {
            	ByteArrayOutputStream baos = new ByteArrayOutputStream();
            	byte[] buf = new byte[1024];
                int ch = -1;    
                int count = 0;    
                while ((ch = is.read(buf)) != -1) {
                	baos.write(buf, 0, ch);    
                	count += ch;    
                    if (length > 0) {
                       int time = (int) ((count / (float) length) * 100);
                             // 如果知道响应的长度，调用publishProgress（）更新进度    
                          publishProgress(time);
                   }        
                }
                        
                byte[] bytes = baos.toByteArray();
                is.close();
                String res = new String(bytes);
                System.out.println("response:" + res);
                return res;
             }
            
		} catch (IOException e) {
			e.printStackTrace();
		} 
    	return null;
	}
	
	@Override
	protected abstract String doInBackground(String... params); 
}
